package com.aischool.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.aischool.params.AiLifePiceseParams;
import com.aischool.utils.StateEnum;
import com.aischool.utils.StringUtil;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class AiLifePiceseService {

	/**
	 * 获取分页数据
	 * @param params
	 * @return
	 */
	public Page<Record> getPage(AiLifePiceseParams params){
		String sql_select = "SELECT a.*, b.user_name, b.user_img ";
		String sql_from   =	"FROM ai_life_pieces a " + 
				"INNER JOIN sys_user b ON b.user_id = a.add_userid ";
		String sql_where  =	"WHERE a.picese_state >= 0 ";
		String sql_order = "ORDER BY a.picese_id DESC ";
				
		List<Object> listObj = new ArrayList<Object>();
		if (params.getState() != StateEnum.ALL.getState()) {
			sql_where = "WHERE a.picese_state = ? ";
			listObj.add(params.getState());
		}
		
		if (params.getTypeId()==2) {
			sql_where += " AND a.is_life = TRUE  ";
		}else if (params.getTypeId()==1) {
			sql_where += " AND a.is_album = TRUE  ";
		}
		
		if (params.getClassId()>0) {
			sql_where += " AND a.class_id = ? ";
			listObj.add(params.getClassId());
		}
		if (params.getStudentId()>0) {
			sql_where += " AND a.student_id = ? ";
			listObj.add(params.getStudentId());
		}
		
		if (params.getReadPower()>0) {
			sql_where += " AND (a.read_power = ? ";
			listObj.add(params.getReadPower());
			if (params.getUserId()>0) {
				sql_where += " OR a.add_userid = ? ";
				listObj.add(params.getUserId());
			}
			sql_where += ") ";
			
		}else if (params.getUserId()>0) {
			sql_where += " AND a.add_userid = ? ";
			listObj.add(params.getUserId());
		}
		
		if (params.getIsTimeLine()) {
			//sql_where += " AND a.add_time <= ? AND a.add_time > ? ";
			//listObj.add(params.getStartDate());
			//listObj.add(params.getEndDate());
			
			List<Object> listObj2 = new ArrayList<Object>();
			
			sql_from += "INNER JOIN (SELECT TO_CHAR(a.add_time, 'YYYY-MM-DD') AS add_date " + 
					"				FROM ai_life_pieces a " +
									sql_where + 
					"				GROUP BY add_date " + 
					"				ORDER BY add_date DESC limit ? OFFSET ?) t1 " + 
					"ON t1.add_date = TO_CHAR(a.add_time, 'YYYY-MM-DD') ";
			
			listObj2.addAll(listObj);
			listObj2.add(params.getPageSize());
			listObj2.add((params.getPageNo()-1)*params.getPageSize());
			listObj2.addAll(listObj);
			
			
			List<Record> list = Db.find(sql_select + sql_from + sql_where + sql_order, listObj2.toArray());
			
			return new Page<Record>(list, params.getPageNo(), params.getPageSize(), 1, 1);
		}else {
			return Db.paginate(params.getPageNo(), params.getPageSize(), sql_select, sql_from + sql_where + sql_order, listObj.toArray());
		}

	}
	
	public Record getOne(int picese_id) {
		return Db.findFirst("SELECT a.* FROM ai_life_pieces a WHERE a.picese_id = ?", picese_id);
	}
	
	public boolean del(int picese_id, int user_id) {
		Record rd = getOne(picese_id);
		if (rd != null && rd.getInt("add_userid") == user_id) {
			int ir = Db.delete("DELETE FROM ai_life_pieces WHERE picese_id = ?", picese_id);
			if (ir > 0) {
				if (StrKit.notBlank(rd.getStr("picese_imgs"))) {
					String strimgs = StringUtil.trim(rd.getStr("picese_imgs"), ';');
					String[] arry1 = strimgs.split(";");
					try {
						for(int j=0;j<arry1.length;j++) {
							String filepath = PathKit.getWebRootPath() + arry1[j];
							File file1 = new File(filepath);
							if (file1.exists()) {
								file1.delete();
							}
						}
					}catch(Exception ex) {
						ex.printStackTrace();
					}
				}
				return true;
			}
		}
		
		return false;
	}
}
